package com.nw.serviceImpl;

import com.nw.dao.AdminDao;
import com.nw.entity.Admin;
import com.nw.service.AdminService;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
@Service
@Transactional

public class AdminServiceImpl implements AdminService {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    AdminDao adminDao;
    @Override
    public HashMap<String, Object> queryByUsername(Admin admin, String enCode) {
        HashMap<String, Object> map = new HashMap<>();
        //获取String类型的操作
        HashOperations hashOperations = stringRedisTemplate.opsForHash();
        String redisCode = (String) hashOperations.get("enCode",enCode);
        if (enCode.equals(redisCode)) {
            map.put("code", "0");
            map.put("msg", "验证码正确");
            if (adminDao.queryByUsername(admin.getUsername()) != null) {
                map.put("code", "0");
                map.put("msg", "用户名正确");
                if (admin.getPassword().equals((adminDao.queryByUsername(admin.getUsername()).getPassword()))){
                    map.put("code", "0");
                    map.put("msg", "密码正确");
                }else {
                    map.put("code", "1");
                    map.put("msg", "密码错误");
                }
            }else {
                map.put("code", "1");
                map.put("msg", "用户名错误");
            }
        }else {
            map.put("code", "1");
            map.put("msg", "验证码错误");
        }
        return map;
    }
}
